package com.excellence.shimmer.Widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by ZhangWei on 2016/6/22.
 */
public class GradientShaderTextView extends TextView
{

	private LinearGradient mLinearGradient;
	private Matrix mGradientMatrix;
	private Paint mPaint;
	private int mViewWidth = 0;
	private int mTranslate = 0;

	private boolean mAnimating = true;
	private int delta = 15;

	public GradientShaderTextView(Context ctx)
	{
		this(ctx, null);
	}

	public GradientShaderTextView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		super.onSizeChanged(w, h, oldw, oldh);
		if (mViewWidth == 0)
		{
			mViewWidth = getMeasuredWidth();
			if (mViewWidth > 0)
			{
				mPaint = getPaint();
				String text = getText().toString();
				// float textWidth = mPaint.measureText(text);
				int size;
				if (text.length() > 0)
				{
					size = mViewWidth * 2 / text.length();
				}
				else
				{
					size = mViewWidth;
				}
				mLinearGradient = new LinearGradient(-size, 0, 0, 0, new int[] { 0x33ffffff, 0xffffffff, 0x33ffffff }, new float[] { 0, 0.5f, 1 }, Shader.TileMode.CLAMP); // 边缘融合
				mPaint.setShader(mLinearGradient);
				mGradientMatrix = new Matrix();
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);

		int length = Math.max(length(), 1);
		if (mAnimating && mGradientMatrix != null)
		{
			float mTextWidth = getPaint().measureText(getText().toString());
			mTranslate += delta;
			if (mTranslate > mTextWidth + 1 || mTranslate < 1)
			{
				delta = -delta;
			}
			mGradientMatrix.setTranslate(mTranslate, 0);
			mLinearGradient.setLocalMatrix(mGradientMatrix);
			postInvalidateDelayed(30);
		}
	}

}
